import 'package:flutter/material.dart';
import '../../../models/device.dart';
import 'device_card.dart';

class DeviceList extends StatelessWidget {
  final List<Device> devices;
  final Function(String, bool) onToggleDevice;
  final Function(String) onDeviceSelected;

  const DeviceList({
    Key? key,
    required this.devices,
    required this.onToggleDevice,
    required this.onDeviceSelected,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (devices.isEmpty) {
      return const Center(
        child: Text(
          '没有可用设备',
          style: TextStyle(fontSize: 16.0),
        ),
      );
    }

    return GridView.builder(
      padding: const EdgeInsets.all(16.0),
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        crossAxisSpacing: 16.0,
        mainAxisSpacing: 16.0,
        childAspectRatio: 1.2,
      ),
      itemCount: devices.length,
      itemBuilder: (context, index) {
        final device = devices[index];
        return DeviceCard(
          device: device,
          onToggle: (status) => onToggleDevice(device.id, status),
          onTap: () => onDeviceSelected(device.id),
        );
      },
    );
  }
}